package cn.lh.service.impl;

import cn.lh.contants.ErrorCode;
import cn.lh.dao.UserDao;
import cn.lh.entity.dto.UserDto;
import cn.lh.entity.req.UserReq;
import cn.lh.entity.vo.UserVo;
import cn.lh.exception.BusinessException;
import cn.lh.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author linhong
 * @since 2023-12-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserDto> implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    //@DSTransactional  切换多数据源时+事务
    public void createUser(UserReq req) {
        LambdaQueryWrapper<UserDto> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserDto::getName,req.getUserName());
        if(this.getOne(wrapper) != null){
            throw new BusinessException(ErrorCode.USER_NAME_EXIST);
        }
        UserDto userDto = req.toDto();
        this.save(userDto);
    }

    @Override
    public void updateUser(UserReq req) {
        if(req.getUserId() == null){
            throw  new BusinessException(ErrorCode.PARAM_ERROR);
        }
        UserDto userDto = req.toDto();
        userDto.setId(req.getUserId());
        this.updateById(userDto);
    }

    @Override
    public List<UserVo> getList(String name) {
        return userDao.getList(name);
    }
}
